package com.xj.driver.mqtt.api;

import com.alibaba.fastjson.JSONObject;
import com.ie.api.center.data.fegin.ControlInfoClient;
import com.ie.api.center.data.fegin.RealDataClient;
import com.xj.common.bean.R;
import com.xj.common.bean.em.ControlStateEm;
import com.xj.common.bean.point.PointValue;
import com.xj.common.model.ControlInfo;
import com.xj.common.model.Point;
import com.xj.common.sdk.bean.driver.DriverContext;
import com.xj.driver.mqtt.service.BusinessService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 *驱动命令对外暴露接口
 * @author wsp
 *
 */
@RestController
@RequestMapping(value = "/business")
public class BusinessAPI {

	@Resource
	private ControlInfoClient controlInfoClient;
	@Resource
	private RealDataClient realDataClient;
	@Resource
	private BusinessService businessService;
	@Resource
	private DriverContext driverContext;

	//创建订单，并启动对应充电机，返回订单编号
	@RequestMapping(value = "/createOrder",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@CrossOrigin
	public Object createOrder(
			@RequestParam(value="userId",required = true,defaultValue = "0") String userId,
			@RequestParam(value="orderTime",required = true,defaultValue = "0") Integer orderTime,
			@RequestParam(value="chargeDevSn",required = true,defaultValue = "0") String chargeDevSn,
			@RequestParam(value="open",required = true,defaultValue = "0") String open
	) throws IOException
	{

/*		//获取传入参数
		String userId = jsonParam.getString("userid");
		int orderTime = jsonParam.getInteger("pre_duration");
		String chargeDevSn = jsonParam.getString("chargeDevSn");*/
		//测试专用
		//orderTime = 360;
		//chargeDevSn = "EC200U";
		//生成订单
		String orderId = businessService.createOrder(userId,orderTime,chargeDevSn);
		//发送控制命令 后面会重构2，这里先把orderid 直接当成handleid
		orderId = businessService.startChargeDev(userId,orderTime,chargeDevSn,open).toString();
		//更新订单
		businessService.updateOrder(orderId,userId,orderTime,chargeDevSn);
		String handle = "";
		Map<String,String> resuMAp = new HashMap<>();
		resuMAp.put("handleid",orderId);
		resuMAp.put("sn",chargeDevSn);

		return R.ok(resuMAp);
	}
	//查询启动命令下发是否成功
	@RequestMapping(value = "/getOrderResult",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	@CrossOrigin
	public Object getOrderResult(
			@RequestParam(value="orderId",required = true,defaultValue = "0") Long orderId
	) throws IOException
	{

		//查询订单信息，得到handle信息,后续会重构，这里直接
		String handle = orderId.toString();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("handle",handle);
		ControlInfo controlInfo = controlInfoClient.handleGetCtlResult(jsonObject).getData();
		if(controlInfo.getResultcode() == ControlStateEm.CTL_STATE_RESPONSE.getCode())
			return R.ok();
		else
			return R.fail();
	}

	//查询当前充电订单实时信息
	@RequestMapping(value = "/getChargeState",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@CrossOrigin
	public Object getChargeState(
			@RequestParam(value="deviceSN",required = true,defaultValue = "0") String orderId
	) throws IOException
	{
		Long deviceid = driverContext.getDriverMetadata().getDeviceSnMap().get(orderId).getId();
		Point soc = driverContext.getPointByDeviceIdAndPointName(deviceid,"SOC");
		Point remainderChgTime = driverContext.getPointByDeviceIdAndPointName(deviceid,"remainderChgTime");
		Point chgTime = driverContext.getPointByDeviceIdAndPointName(deviceid,"chgTime");
		Point chgOutputCurr = driverContext.getPointByDeviceIdAndPointName(deviceid,"chgOutputCurr");
		Point chgOutputVolt = driverContext.getPointByDeviceIdAndPointName(deviceid,"chgOutputVolt");
		Point chargeState = driverContext.getPointByDeviceIdAndPointName(deviceid,"chargeState");
		JSONObject jsonObject0 = new JSONObject();
		jsonObject0.put("deviceId",deviceid);
		jsonObject0.put("pointId",soc.getId());
		jsonObject0.put("history",false);
		PointValue pvsoc = realDataClient.handleGetRealDataByDeviceIdAndPointID(jsonObject0).getData();
		JSONObject jsonObject1 = new JSONObject();
		jsonObject1.put("deviceId",deviceid);
		jsonObject1.put("pointId",remainderChgTime.getId());
		jsonObject1.put("history",false);
		PointValue pvremainderChgTime = realDataClient.handleGetRealDataByDeviceIdAndPointID(jsonObject1).getData();
		JSONObject jsonObject2 = new JSONObject();
		jsonObject2.put("deviceId",deviceid);
		jsonObject2.put("pointId",chgTime.getId());
		jsonObject2.put("history",false);
		PointValue pvchgTime = realDataClient.handleGetRealDataByDeviceIdAndPointID(jsonObject2).getData();
		JSONObject jsonObject3 = new JSONObject();
		jsonObject3.put("deviceId",deviceid);
		jsonObject3.put("pointId",chgOutputCurr.getId());
		jsonObject3.put("history",false);
		PointValue pvchgOutputCurr = realDataClient.handleGetRealDataByDeviceIdAndPointID(jsonObject3).getData();
		JSONObject jsonObject4 = new JSONObject();
		jsonObject4.put("deviceId",deviceid);
		jsonObject4.put("pointId",chgOutputVolt.getId());
		jsonObject4.put("history",false);
		PointValue pvchgOutputVolt = realDataClient.handleGetRealDataByDeviceIdAndPointID(jsonObject4).getData();
		JSONObject jsonObject5 = new JSONObject();
		jsonObject5.put("deviceId",deviceid);
		jsonObject5.put("pointId",chargeState.getId());
		jsonObject5.put("history",false);
		PointValue pvchargeState = realDataClient.handleGetRealDataByDeviceIdAndPointID(jsonObject5).getData();

		Map<String ,String > resultMap = new HashMap<>();
		resultMap.put("soc",pvsoc==null? "96" : pvsoc.getRawValue());
		resultMap.put("remainderChgTime",pvremainderChgTime==null ? "00:15:23":pvremainderChgTime.getRawValue());
		resultMap.put("chgTime",pvchgTime==null ? "1:02:00":pvchgTime.getRawValue());
		resultMap.put("chgOutputCurr",pvchgOutputCurr==null? "138":pvchgOutputCurr.getRawValue());
		resultMap.put("chgOutputVolt",pvchgOutputVolt==null? "0.8":pvchgOutputVolt.getRawValue());
		resultMap.put("chargeState",pvchargeState==null? "充电中":pvchargeState.getRawValue());

		return R.ok(resultMap);

	}
}
